﻿using DBEntitie;
using FrameWork;
using lhservice.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Threading.Tasks;
using lhservice.common;
using Hangfire;
using lhservice.common.websocket;
using System.Reflection;
using System.Configuration;
using RestSharp;

namespace lhservice.Controllers
{ 
  [ApiController]
  [Route("api/[controller]")]
  [AllowAnonymous]
  public class OpenController : TimController
  {    
    [HttpPost("[action]")]
    public async Task<IActionResult> Ver()
    {      
      return await PublicTool.ReturnSuccess("20220405");      
    }

    [HttpGet("[action]")]
    public async Task<IActionResult> T(string cid)
    {
      return await PublicTool.ReturnSuccess(RegHelper.GetRegInfo(cid));
    }

    [HttpGet("[action]")]
    public async Task<IActionResult> sms(string to, string tempId, string paramLis)
    {
      var s = SmsHelper.Send(to, tempId, paramLis);    
      return await PublicTool.ReturnSuccess(s);
    }

    [AllowAnonymous]
    [HttpGet("[action]")]
    public async Task<IActionResult> GetOpenid(string codes)
    {
      var appid = "wxa1a2217f19385afc";
      var secret = "ee7e2409c66a9e6a12a0de8d6b951f80";
      var url = $"https://api.weixin.qq.com/sns/jscode2session?appid={appid}&secret={secret}&js_code={codes}&grant_type=authorization_code";
      var client = new RestClient();
      var request = new RestRequest(url);
      Task<RestResponse> t = client.ExecuteGetAsync(request);
      t.Wait();
      var restResponse = await t;
      var res = JsonConvert.DeserializeObject<Dictionary<string,dynamic>>(restResponse.Content);
      return await PublicTool.ReturnSuccess(res["openid"]);
    }
  }
}
